define(['jquery', 'bootstrap', 'backend', 'table', 'form','cookie'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'fas/zcdiaobojl/index' + location.search,
                    add_url: 'fas/zcdiaobojl/add',
                    edit_url: 'fas/zcdiaobojl/edit',
                    del_url: 'fas/zcdiaobojl/del',
                    multi_url: 'fas/zcdiaobojl/multi',
                    import_url: 'fas/zcdiaobojl/import',
                    table: 'fas_zcdiaobojl',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                search:false,
                showToggle: false,
                showExport: false,
                searchFormVisible: true,
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        //{checkbox: true},
                        //{field: 'id', title: __('Id')},
                        //{field: 'zcdevice_id', title: __('Zcdevice_id')},
                        {field: 'zcdevice.name', title: __('Zcdevice.name')},
                        {field: 'fgroup', title: __('Fgroup')},
                        {field: 'ffactory', title: __('Ffactory')},
                        {field: 'sydw', title: __('Sydw')},
                        {field: 'sydd', title: __('Sydd')},
                        {field: 'fzr', title: __('Fzr'), operate:false},
                        {field: 'date', title: __('Date'), operate:false},
                        {field: 'fgroup0', title: __('Fgroup0'), operate:false},
                        {field: 'ffactory0', title: __('Ffactory0'), operate:false},
                        {field: 'sydw0', title: __('Sydw0'), operate:false},
                        {field: 'sydd0', title: __('Sydd0'), operate:false},
                        {field: 'fzr0', title: __('Fzr0'), operate:false},
                        {field: 'sqr', title: __('Sqr'), operate:false},
                        {field: 'state', title: __('State'), defaultValue:"0", searchList: {"0":__('State 0'),"1":__('State 1')}, formatter: Table.api.formatter.normal},
                        //{field: 'create_time', title: __('Create_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        //{field: 'update_time', title: __('Update_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        //{field: 'zcdevice.id', title: __('Zcdevice.id')},
                        //{field: 'zcdevice.zctype_id', title: __('Zcdevice.zctype_id')},
                        //{field: 'zcdevice.zcorder_id', title: __('Zcdevice.zcorder_id')},
                        //{field: 'zcdevice.sbsynx', title: __('Zcdevice.sbsynx')},
                        //{field: 'zcdevice.sbjz', title: __('Zcdevice.sbjz')},
                        //{field: 'zcdevice.sydw', title: __('Zcdevice.sydw')},
                        //{field: 'zcdevice.sydd', title: __('Zcdevice.sydd')},
                        //{field: 'zcdevice.images', title: __('Zcdevice.images'), formatter: Table.api.formatter.images},
                        //{field: 'zcdevice.attachfiles', title: __('Zcdevice.attachfiles')},
                        //{field: 'zcdevice.qydate', title: __('Zcdevice.qydate'), operate:'RANGE', addclass:'datetimerange'},
                        //{field: 'zcdevice.bfdate', title: __('Zcdevice.bfdate'), operate:'RANGE', addclass:'datetimerange'},
                        //{field: 'zcdevice.cz', title: __('Zcdevice.cz'), operate:'BETWEEN'},
                        //{field: 'zcdevice.state', title: __('Zcdevice.state')},
                        //{field: 'zcdevice.create_time', title: __('Zcdevice.create_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        //{field: 'zcdevice.update_time', title: __('Zcdevice.update_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 选择隐藏字段事件，使用cookie存入
            table.on('column-switch.bs.table', function (e, json) {
                    var myColumns=[];
                    $.each(table.bootstrapTable('getHiddenColumns'), function(i, item) {
                         myColumns.push(item.field);
                    });
                    $.cookie('fa_fas_zcdiaobojl', JSON.stringify(myColumns), { expires: 365, path: '/'});
             });
             if($.cookie('fa_fas_zcdiaobojl')){
                //读取cookie隐藏字段
                $.each(JSON.parse($.cookie('fa_fas_zcdiaobojl')), function(i, item) {
                  table.bootstrapTable('hideColumn', item);
                });
             }

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});